package com.cloud.class04.controller;

import cn.hutool.core.date.DateUtil;
import com.cloud.class04.apis.PayFeignApi;
import com.cloud.class04.entities.PayDTO;
import com.cloud.class04.resp.ResultData;
import com.cloud.class04.resp.ReturnCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@ResponseBody
@Slf4j
public class OrderController {
    @Autowired
    private PayFeignApi payFeignApi;

    //    @PostMapping("/feign/pay/add")
    @RequestMapping(value = "/feign/pay/add", method = {RequestMethod.POST})
    public ResultData addOrder(PayDTO payDTO) {
//        System.out.println();
        log.info("第一步：模拟本地addOrder新增订单成功(省略sql操作)，第二步：再开启addPay支付微服务远程调用");
        return payFeignApi.addPay(payDTO);
    }

    @GetMapping("/feign/pay/get/{id}")
    public ResultData getPayInfo(@PathVariable("id") Integer id) {
        log.info("-------支付微服务远程调用，按照id查询订单支付流水信息");
        ResultData resultData = null;

        try {
            log.info("-------调用开始-------------" + DateUtil.now());
            resultData = payFeignApi.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("-------调用结束-------------" + DateUtil.now());
            ResultData.fail(ReturnCodeEnum.RC500.getCode(), e.getMessage());
        }

        return resultData;
    }

    @GetMapping("/feign/pay/mylb")
    public String getPayInfoByConsul() {
        return payFeignApi.getInfoByConsul();
    }


}
